/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2018 All Rights Reserved.
 */
package com.ruoyi.SuveryInterface.SuveryUtils.util.bestPayUtils.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.SuveryInterface.SuveryUtils.util.bestPayUtils.handler.BestpayHandler;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.List;

/**
 * @author yangbo2018
 * @version Id: BestpayServiceTemp.java, v 0.1 2018/3/2 11:35 yangbo2018 Exp $$
 */
public abstract class BestpayServiceTemp {
    private BestpayHandler bestpayHandler;

    public BestpayHandler getBestpayHandler() {
        return bestpayHandler;
    }

    public void setBestpayHandler(BestpayHandler bestpayHandler) {
        this.bestpayHandler = bestpayHandler;
    }

    public final void invoke(String url, Object object, String platformCode, String iv, List<String> list) throws Exception {

        String request=doConvert(object,platformCode,iv);
        System.out.println("请求翼支付报文："+request);
        long startTime = System.currentTimeMillis();
        String response=doService(url,request);
        int costTime = (int)(System.currentTimeMillis() - startTime);
        list.add(String.valueOf(costTime));
        JSONObject json = JSONObject.parseObject(response);

        String code = null;
        String message = null;
        code = json.getString("errorCode");
        message = json.getString("errorMsg");

        if ("000000".equals(code)) {
            JSONObject resultJson = json.getJSONObject("result");
            code = resultJson.getString("stat");
            message = resultJson.getString("desc");
        }
        list.add(code);
        list.add(message);
        System.out.println("翼支付响应报文："+response);
        boolean verifyResult=doVerify(response);
        System.out.println("验签结果:"+verifyResult);
    }
    protected abstract String doConvert(Object object,String platformCode,String iv) throws Exception;
    protected abstract boolean doVerify(String response);

    private String doService(String Url, String request) {
        System.out.println("请求翼支付的URL地址："+Url);
        System.out.println("发送给翼支付的http请求报文:" + request);
        HttpPost httpPost = new HttpPost(Url);
        httpPost.setHeader("Content-Type","application/json");
        StringEntity se = new StringEntity(request, "utf-8");
        httpPost.setEntity(se);
        String responseStr=null;
        CloseableHttpClient closeableHttpClient= HttpClients.createDefault();
        try {
            HttpResponse httpResponse=closeableHttpClient.execute(httpPost);
            int code=httpResponse.getStatusLine().getStatusCode();
            System.out.println("Http状态码："+code);
            if(200==code){
                responseStr = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
                System.out.println("从翼支付收到的http响应报文:"+responseStr);
            }
            closeableHttpClient.close();

        }catch (Exception e){
            e.printStackTrace();
        }
        return responseStr;
    }

}
